<template>
  <div class="order-container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 编辑商品弹窗 -->
    <el-dialog title="修改商品" :visible.sync="editDislog" width="30%">
      <el-form :model="editGoods" :rules="editRules" ref="editForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品编号" prop="order_number">
          <el-input v-model="editGoods.order_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="order_price">
          <el-input v-model="editGoods.order_price"></el-input>
        </el-form-item>
        <el-form-item label="是否支付" prop="pay_status">
          <el-input v-model="editGoods.pay_status"></el-input>
        </el-form-item>
        <el-form-item label="是否发货" prop="is_send">
          <el-input v-model="editGoods.is_send"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDislog = false">取 消</el-button>
        <el-button type="primary" @click="editAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 卡片区域 -->
    <el-card shadow="always">
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="10"><el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="searchQuery"></el-button>
          </el-input></el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsOrder" stripe style="width: 100%" :border="true">
        <el-table-column type="index" label="#" align="center">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号" align="center">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" align="center">
        </el-table-column>
        <el-table-column label="是否付款" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.pay_status === '0' ? 'danger' : 'success'">{{ scope.row.pay_status === '0' ?
              '未付款' : '已付款'
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" align="center">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间" align="center">
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="editProps(scope.row)"></el-button>
            <el-button type="success" icon="el-icon-location" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 1
      },
      goodsOrder: [], // 商品信息
      total: 0, // 信息条数
      editGoods: {
        order_number: '',
        order_price: '',
        is_send: '',
        pay_status: 0
      },
      editRules: {
        order_price: [{ required: true, message: '请输入商品价格·', trigger: 'blur' }],
        pay_status: [{ required: true, message: '请输入支付状态·', trigger: 'blur' }],
        is_send: [{ required: true, message: '请输入发货状态·', trigger: 'blur' }]
      },
      editDislog: false,
      seletId: ''
    }
  },
  created() {
    this.getGoodsOrder()
  },
  methods: {
    async getGoodsOrder() {
      const { data: res } = await this.$http.get('/orders', { params: this.queryInfo })
      // 对响应回来的数据进行校验
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.goodsOrder = res.data.goods
      this.total = res.data.total
    },
    // 监听页数变化
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getGoodsOrder()
    },
    // currentPage 改变时会触发
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getGoodsOrder()
    },
    // 查询
    searchQuery() {
      this.getGoodsOrder()
    },
    // 修改
    editProps(val) {
      this.seletId = val.order_id
      this.editGoods.order_number = val.order_number
      this.editGoods.order_price = val.order_price
      this.editGoods.is_send = val.is_send
      this.editGoods.pay_status = val.pay_status
      this.editDislog = true
    },
    editAdd() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) {
          this.$message.error('请完善信息')
        } else {
          const { data: res } = await this.$http.put('/orders/' + this.seletId, this.editGoods)
          // 对相应回来的数据进行校验
          if (res.meta.status !== 201) return this.$message.error('添加失败')
          this.getGoodsOrder()
          this.editDislog = false
          return this.$message.success('更新状态成功')
        }
      })
    }
  }
}

</script>

<style lang="less" scoped>
.order-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .el-table {
    margin: 20px 0;
    font-size: 13px;
  }
}
</style>
